package services;

import helpers.UrlHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gdata.data.PlainTextConstruct;

import redstone.xmlrpc.XmlRpcArray;
import redstone.xmlrpc.XmlRpcFault;

import net.bican.wordpress.Category;
import net.bican.wordpress.Page;
import net.bican.wordpress.Wordpress;
import models.Blog;
import models.News;

public class WordPress {
	private String xmlRpcUrl = "";
	
	private String username = "";
	
	private String password = "";
	
	private String blogName = "";
	
	private Wordpress wp;
	
	public WordPress(Blog blog) {
		xmlRpcUrl = blog.getBlogId() + "/xmlrpc.php";
		username = blog.getUsername();
		password = blog.getPassword();
		blogName = blog.getBlogName();
		try {
			wp = new Wordpress(username, password, xmlRpcUrl);
		} catch (Exception e) {
		}
		
	}
	
	public int createPost(News news) {
		int id = 0;
		try {
			Page recentPost = new Page();
			recentPost.setDateCreated(new Date());
			String status = news.getPublished() ? "publish" : "draft";
		    recentPost.setPost_status(status);
		    recentPost.setWp_author("admin");
		    recentPost.setTitle(new PlainTextConstruct(news.getTitle()).getText());
		    String content = news.getDescription();
		    if (news.isDisplaySource()) {
			  content += "<div style=\"text-align: center;\"><b>Source:</b> " + UrlHelper.getDomainName(news.getUrl()) + "</div>";
		    }
		    recentPost.setDescription(new PlainTextConstruct(content).getText());
		    List<String> labels = news.getLabels();
		    
		    //Check exists and add category
		    XmlRpcArray categories = checkCategories(labels);
		    
		    //Set category to post
		    recentPost.setCategories(categories);
		    /*String keywords = "hoanbn, hoantocxoan, 123";
		    recentPost.setMt_keywords(keywords);*/
			String postId = wp.newPost(recentPost, false);
			id = Integer.parseInt(postId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public boolean deletePost(int postId) {
		boolean success = false;
		try {
			success = wp.deletePost(postId, "false");
		} catch (XmlRpcFault e) {
			// TODO Auto-generated catch block
		}
		return success;
	} 
	
	private XmlRpcArray checkCategories(List<String> labels) {
		XmlRpcArray result = new XmlRpcArray();
		List<Category> categories = getCategories();
		boolean existsCat = false;
		for (int i = 0; i < labels.size(); i++) {
			for (int j = 0; j < categories.size(); j++) {
				if (labels.get(i).equalsIgnoreCase(categories.get(j).getCategoryName())) {
					existsCat = true;
				}
			}
			
			if (!existsCat) {
				try {
					int catId = wp.newCategory(labels.get(i), "", 0);
				} catch (XmlRpcFault e) {
				}
			}
			existsCat = false;
			result.add(labels.get(i));
		}
		return result;
	}
	
	public List<Category> getCategories() {
		List<Category> categories = new ArrayList<Category>();
		try {
			categories = wp.getCategories();
		} catch (XmlRpcFault e) {
			//e.printStackTrace();
		}
		
		return categories;
	}
	
	public static void main(String[] args) {
		Blog blog = new Blog();
		blog.setBlogId("http://phpmysqltutorial.com/");
		blog.setUsername("admin");
		blog.setPassword("abcD1234");
		WordPress wp = new WordPress(blog);
//		wp.createPost();
	}
}
